package org.chench.extra.test.servletcontainer.controller;

import org.chench.extra.test.servletcontainer.vo.ComplexObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 压测示例
 * @author: nuccch2010@163.com
 * @desc: org.chench.extra.test.servletcontainer.controller.SampleController
 * @date: 2020/3/29 22:36
 */
@RestController
@RequestMapping("/sample")
public class SampleController {
    private Random random = new Random();

    /**
     * 返回简单字符串对象
     * @return
     */
    @GetMapping("/str")
    public Object str() {
        return UUID.randomUUID().toString();
    }

    /**
     * 返回复杂对象
     * @return
     */
    @GetMapping("/complex")
    public Object complex() {
        String name = UUID.randomUUID().toString();
        Integer type = random.nextInt(1000);
        Long parent = random.nextLong();
        int size = random.nextInt(30);
        List<Object> children = new ArrayList<Object>(size);
        for (int i = 0; i < size; i++ ) {
            if ((i % 2) == 0 ) {
                children.add(random.nextDouble());
            } else if ((i % 3) == 0) {
                children.add(random.nextLong());
            } else {
                children.add(random.nextInt(1000));
            }
        }
        Date ctime = new Date(System.currentTimeMillis());
        Date mtime  = new Date(System.currentTimeMillis());
        Double price = random.nextDouble() * random.nextInt(100);
        String unit = UUID.randomUUID().toString();
        return new ComplexObject(name, type, parent, children, ctime, mtime, price, unit);
    }

    // 模拟进程遇到异常情况退出生成core文件
    private List<Object> list = new ArrayList<Object>();
    @GetMapping("/core")
    public Object generateCoreFile() {
        System.out.println("generate core file start.");
        for(;;) {
            String name = UUID.randomUUID().toString();
            Integer type = random.nextInt(1000);
            Long parent = random.nextLong();
            int size = random.nextInt(30);
            List<Object> children = new ArrayList<Object>(size);
            for (int i = 0; i < size; i++ ) {
                if ((i % 2) == 0 ) {
                    children.add(random.nextDouble());
                } else if ((i % 3) == 0) {
                    children.add(random.nextLong());
                } else {
                    children.add(random.nextInt(1000));
                }
            }
            Date ctime = new Date(System.currentTimeMillis());
            Date mtime  = new Date(System.currentTimeMillis());
            Double price = random.nextDouble() * random.nextInt(100);
            String unit = UUID.randomUUID().toString();
            list.add(new ComplexObject(name, type, parent, children, ctime, mtime, price, unit));
        }
    }
}
